package com.kw.mybatistest.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.kw.mybatistest.entity.Blog;
import com.kw.mybatistest.entity.User;
import com.kw.mybatistest.mapper.BlogMapper;
import com.kw.mybatistest.mapper.UserMapper;
import com.kw.mybatistest.vo.BlogTagListVo;
import com.kw.mybatistest.vo.BlogVo;
import com.kw.mybatistest.vo.UserVo;

public class SqlSessionFactoryUtil {
	
	public static SqlSessionFactory getSqlSessionFactory() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		return sqlSessionFactory;
	}

	public static User selectUser() throws IOException{
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  long beforeExecutorTime = System.currentTimeMillis();
		  UserMapper userMapper = (UserMapper) session.getMapper(UserMapper.class);
		  User user = userMapper.selectUser(1L);
		  System.out.println(user);
		  long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("consume : "+(afterExecutorTime-beforeExecutorTime) +"ms");
		  return user;
		} finally {
		  session.close();
		}
	}
	
	public static void selectUserReturnMap() throws IOException{
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  long beforeExecutorTime = System.currentTimeMillis();
		  UserMapper userMapper = (UserMapper) session.getMapper(UserMapper.class);
		  UserVo user = userMapper.selectUserReturnMap(1L);
		  System.out.println(user);
		  long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("consume : "+(afterExecutorTime-beforeExecutorTime) +"ms");
		} finally {
		  session.close();
		}
	}
	
	public static void testCache()throws IOException{
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  long beforeExecutorTime = System.currentTimeMillis();
		  UserMapper userMapper = (UserMapper) session.getMapper(UserMapper.class);
		  User user = userMapper.selectUserByUsername("keithwang");
		  System.out.println(user);
		  long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("consume : "+(afterExecutorTime-beforeExecutorTime) +"ms");
		  
		  beforeExecutorTime = System.currentTimeMillis();
		  userMapper.selectUserByUsername("keithwang");
		  afterExecutorTime = System.currentTimeMillis();
		  System.out.println("consume : "+(afterExecutorTime-beforeExecutorTime) +"ms");
		} finally {
		  session.close();
		}
	}
	
	public static void testTwoLevelCache(int time)throws IOException{
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  long beforeExecutorTime = System.currentTimeMillis();
		  UserMapper userMapper = (UserMapper) session.getMapper(UserMapper.class);
		  User user = userMapper.selectUserByUsername("keithwang");
		  System.out.println(user);
		  long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("consume : "+(afterExecutorTime-beforeExecutorTime) +"ms at "+time+" times");
		} finally {
		  session.close();
		}
	}
	
	public static void main(String[] args) throws IOException {
		//selectUser();
		//testCache();
		//selectUserReturnMap();
		//selectBlogById();
		//selectBlogDetailById();
		
		selectBlogTagListById();
		
		testTwoLevelCache(1);
		testTwoLevelCache(2);
		testTwoLevelCache(3);
		testTwoLevelCache(4);
	}
	
	public static void selectBlogById() throws IOException {
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  //long beforeExecutorTime = System.currentTimeMillis();
		  BlogMapper userMapper = (BlogMapper) session.getMapper(BlogMapper.class);
		  Blog blog = userMapper.selectBlogById(1L);
		  System.out.println(blog);
		  //long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("selectBlogById blog : "+blog);
		} finally {
		  session.close();
		}
	}
	
	public static void selectBlogDetailById() throws IOException {
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  //long beforeExecutorTime = System.currentTimeMillis();
		  BlogMapper userMapper = (BlogMapper) session.getMapper(BlogMapper.class);
		  BlogVo blog = userMapper.selectBlogDetailById(1L);
		  System.out.println(blog);
		  //long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("selectBlogDetailById blog : "+blog);
		} finally {
		  session.close();
		}
	}
	
	public static void selectBlogTagListById() throws IOException {
		SqlSession session = getSqlSessionFactory().openSession();
		try {
		  //long beforeExecutorTime = System.currentTimeMillis();
		  BlogMapper userMapper = (BlogMapper) session.getMapper(BlogMapper.class);
		  BlogTagListVo blog = userMapper.selectBlogTagListVolById(1L);
		  //System.out.println(blog);
		  //long afterExecutorTime = System.currentTimeMillis();
		  System.out.println("selectBlogTagListById blog : "+blog);
		} finally {
		  session.close();
		}
	}
}
